package cn.youwan.market.http;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import cn.youwan.market.utils.Const;
import cn.youwan.market.utils.UserPreference;
import cn.youwan.market.utils.Utils;


import android.content.Context;

public class YouwanSession {
	private static YouwanSession yangSession;
	private Context context;

	private YouwanSession(Context context) {
		this.context = context;
		ProcessSource();
		ProcessHost();
	}

	public static YouwanSession getYangSession(Context context) {
		if (yangSession == null) {
			yangSession = new YouwanSession(context);
		}
		return yangSession;
	}

	private String userName;
	private String userkey;
	private String imea; // imea号
	private String verName; // 版本号
	private String model; // 手机型号
	private String Os;// 系统版本号
	private String source; // 渠道号

	public String getSource() {
		return source;
	}

	public String getOs() {
		if (Os == null) {
			Os = android.os.Build.VERSION.RELEASE;
		}
		return Os;
	}

	public String getModel() {
		if (model == null) {
			model = android.os.Build.MODEL;
		}
		return model;
	}

	public String getVerName() {
		verName = UserPreference.read("appversion", "");
		return verName;
	}

	public String getImea() {
		if (imea == null) {
			imea = Utils.getImei(context);
		}
		return imea;
	}

	public String getUserKey() {
		if (userkey == null) {
			UserPreference.ensureIntializePreference(context);
			userkey = UserPreference.read("userkey", "");
		}
		return userkey;
	}

	public String getUserName() {
		if (userName == null) {
			UserPreference.ensureIntializePreference(context);
			userName = UserPreference.read("username", "");
		}
		return userName;
	}

	// 读取渠道号
	private void ProcessSource() {
		String result = "";
		try {
			InputStream in = this.context.getResources().getAssets()
					.open("source.txt");
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(in, "utf-8"));
			result = bufferedReader.readLine();
			result = result.substring(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result != null && !result.equals("")) {
			source = result;
		}
	}

	// 读取域名
	public void ProcessHost() {
		String result = "";
		try {
			InputStream in = this.context.getResources().getAssets()
					.open("host.txt");
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(in, "utf-8"));
			result = bufferedReader.readLine();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result != null && !result.equals("")) {
			Const.Host = result;
		}
	}
}
